package com.game.manager.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.game.manager.dao.entity.GmMenu;
import com.game.manager.dao.entity.GmPermission;
import com.game.manager.vo.VOPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.game.manager.dao.entity.model.MenuNode;
import com.game.manager.dao.entity.model.Permission;
import com.game.manager.service.CommonService;
import com.game.manager.service.GmMenuService;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Autowired
    private GmMenuService menuService;
    @Autowired
    private CommonService commonService;
    @GetMapping("getMenu")
    public Object getMenu() {
        GmMenu gmMenu = menuService.getGmMenu();
        JSONObject result = new JSONObject();
        result.put("total", gmMenu.getMenuNodes().size());
        result.put("rows", gmMenu.getMenuNodes());
        return result;
    }
    @GetMapping("getMenuList")
    public Object getMenuList() {
        GmMenu gmMenu = menuService.getGmMenu();
       List<MenuNode> menuNodes = menuService.getMenuNodeList(gmMenu);
       MenuNode menuNode = new MenuNode();
       menuNode.setId("-1");
       menuNode.setText("--请选择的父菜单--");
       menuNode.setSelected(true);
       List<MenuNode> result = new ArrayList<>();
       result.add(menuNode);
       result.addAll(menuNodes);
       return result;
        
    }
    @PostMapping("addMenu")
    public Object addMenu(@RequestBody MenuNode menuNode) {
        GmMenu menu = menuService.getGmMenu();
        menuService.addOrUpdateMenu(menu, menuNode);
        return commonService.response();
    }
    @PostMapping("deleteMenu/{menuId}")
    public Object deleteMenu(@PathVariable String menuId) {
        GmMenu menu = menuService.getGmMenu();
        menuService.deleteMenu(menu, menuId);
        return commonService.response();
    }
    
    @GetMapping("getPermission")
    public Object getPermission() {
        List<VOPermission> voPermissions = menuService.getPermission();
        return commonService.responseTable(voPermissions, voPermissions.size());
    }
    @GetMapping("getPermissionByMenuId/{menuId}")
    public Object getPermissionByMenuId(@PathVariable String menuId) {
        if(menuId.equals("-1")) {
            return Collections.emptyList();
        }
        GmPermission gmPermission = menuService.getGmPermission(menuId);
        if(gmPermission == null) {
            return Collections.emptyList();
        }
        return gmPermission.getChildren();
    }
    @PostMapping("deletePermission/{pid}/{menuId}")
    public Object deletePermission(@PathVariable String pid,@PathVariable String menuId) {
        menuService.deletePermission(menuId, pid);
        return commonService.response();
    }
    @PostMapping("addPermission")
    public Object addPermission(@RequestBody Permission permission) {
        menuService.addPermission(permission.getParentId(), permission);
        return commonService.response();
    }

    @PostMapping("activity/manager")
    public Object activityManager(@RequestBody Permission permission) {
        menuService.addPermission(permission.getParentId(), permission);
        return commonService.response();
    }
}
